package me.sunpeng.kafka.producer;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * 自定义分区规则
 * @author sp
 * @create 2022-03-17
 */
public class MyPartitioner implements Partitioner {

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {

        //获取数据 hello
        String msgValues = value.toString();

        int parttion;

        if (msgValues.contains("hello")){
            parttion = 0;
        }else {
            parttion = 1;
        }

        return parttion;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
